<?php 
// 加载功能函数文件
require_once('helpers.php');



$conn = @mysqli_connect('localhost','wd1900144','pass123456') or exit('网络开小差了,请稍后再试');
mysqli_select_db($conn,'wd1900144');
mysqli_query($conn,'set names utf8');

$act = $_REQUEST['act'];	// $_REQUEST 可以接收$_POST或者$_GET的数据

//
if($act=='add'){
	unset($_GET['act']);

	//$fid 是主键的值
	$did = insert('car_news',$_GET);	
	echo $did>0 ? json_encode(['error'=>0,'msg'=>'添加成功']) : json_encode(['error'=>1,'msg'=>'网络出错']);
}

//查看新闻信息 
else if($act=='list'){
	
	$limit = $_GET['limit'];	// 每页显示多少条
	$pn = $_GET['pn'];	// 页码
	$start = ($pn-1)*$limit;
	// 获取分类列表数据
	$state = $_GET['state'];
	$cid = $_GET['cid'];
	$cate=$_GET['cate'];
	$allist = array();
	if($cid==0){
		if($cate=='recruit'){
			$tab = getAll("select * from car_cate where 1 limit 3,2");
		}else{
			$tab = getAll("select * from car_cate where 1 limit 0,3");
		}
		// var_dump($tab[0]['cid']);

		foreach($tab as $k => $v ){
			$cid = $v['cid'];
			if($state == 'admin'){
				$list = getAll("select * from car_news where cid='$cid' limit $start,$limit");
			}else $list = getAll("select * from car_news where cid='$cid' && state='1' limit $start,$limit");
			// array_reverse($list);
			
			array_push($allist,array_reverse($list));
		}
		$is = 'default';
		}else{
			if($admin == 'admin'){
				$list = getAll("select * from car_news where cid='$cid' limit $start,$limit");}
				else $allist = getAll("select * from car_news where cid='$cid' && state='1'  limit $start,$limit");
				
			$count = getCount('car_news','cid='.$cid);
			$is = 'update';
		}

	echo json_encode(['error'=>0,'msg'=>'success','list'=>$allist,'cate'=>$tab,'is'=>$is,'count'=>$count]);
	
	
}


else if($act=='detail'){
	
		$nid = $_GET['nid'];
		$detail = getOne("select * from car_news where nid='$nid'");
		echo json_encode(['error'=>0,'msg'=>'success','detail'=>$detail]);
		
}
else if($act=='edit'){
		unset($_GET['act']);
		$state = $_GET['state'];
		$nid = $_GET['nid'];
		$res = update('car_news',$_GET,"nid=$nid");
		
		 echo $res>0 ? json_encode(['error'=>0,'msg'=>'操作成功']) : json_encode(['error'=>1,'msg'=>'未操作']);
}
else if($act=='del'){
		unset($_GET['act']);
		$nid = $_GET['nid'];
		$res = delete('car_news',"nid=$nid");
		
		 echo $res>0 ? json_encode(['error'=>0,'msg'=>'退回成功']) : json_encode(['error'=>1,'msg'=>'未退回']);
}


mysqli_close($conn);

 ?>